package com.itsu.mysql.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itsu.interfaces.entity.UserInfoEntity;
import com.itsu.interfaces.service.UserInfoService;
import com.itsu.mysql.mappers.UserInfoMapper;
import com.itsu.pojo.dto.RecommendationDTO;
import com.itsu.pojo.vo.LinearVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public void addUserInfo(UserInfoEntity userInfoEntity) {
        userInfoMapper.insert(userInfoEntity);

    }

    @Override
    public void updateUserLogo(UserInfoEntity userInfoEntity) {
        userInfoMapper.update(userInfoEntity, Wrappers.lambdaUpdate(UserInfoEntity.class)
                .eq(UserInfoEntity::getUserId, userInfoEntity.getUserId()));
    }

    @Override
    public UserInfoEntity findUserByUid(Long uid) {
        return userInfoMapper.selectOne(Wrappers.lambdaQuery(UserInfoEntity.class)
                .eq(UserInfoEntity::getUserId, uid));

    }

    @Override
    public Page<UserInfoEntity> findUserListOfRecommendation(List<Long> userIds, RecommendationDTO recommendationDTO) {
        Page<UserInfoEntity> page = new Page<>(recommendationDTO.getPage() <= 0 ? 0 : recommendationDTO.getPage() - 1, recommendationDTO.getPagesize());
        userInfoMapper.selectPage(page, Wrappers.lambdaQuery(UserInfoEntity.class)
                        .in(UserInfoEntity::getUserId, userIds)
//                .eq(ObjectUtil.isNotNull(recommendationDTO.getGender()), UserInfoEntity::getSex, "woman".equals(recommendationDTO.getGender())? 1 : 2 )
//                .le(ObjectUtil.isNotNull(recommendationDTO.getAge()), UserInfoEntity::getAge, recommendationDTO.getAge())
//                .eq(ObjectUtil.isNotNull(recommendationDTO.getCity()), UserInfoEntity::getCity, recommendationDTO.getCity())
//                .eq(ObjectUtil.isNotNull(recommendationDTO.getEducation()), UserInfoEntity::getEdu, recommendationDTO.getEducation())
        );
        return page;
    }

    @Override
    public Page<UserInfoEntity> pageUsers(Integer page, Integer pageSize, List<Long> userIds) {
        return userInfoMapper.selectPage(new Page<>(page, pageSize),
                Wrappers.lambdaQuery(UserInfoEntity.class)
                        .in(UserInfoEntity::getUserId, userIds));
    }

    @Override
    public Page<UserInfoEntity> pageUsers(Integer page, Integer pageSize, List<Long> userIds, String keyword) {
        return userInfoMapper.selectPage(new Page<>(page, pageSize),
                Wrappers.lambdaQuery(UserInfoEntity.class)
                        .like(UserInfoEntity::getNickName, keyword)
                        .in(UserInfoEntity::getUserId, userIds));
    }

    @Override
    public Page<UserInfoEntity> pageUsers(Integer page, Integer pageSize, Long uid, String nickName, String city) {
        return userInfoMapper.selectPage(new Page<>(page, pageSize),
                Wrappers.lambdaQuery(UserInfoEntity.class)
                        .eq(ObjectUtil.isNotEmpty(uid), UserInfoEntity::getUserId, uid)
                        .eq(StrUtil.isNotBlank(nickName), UserInfoEntity::getNickName, nickName)
                        .eq(StrUtil.isNotBlank(city), UserInfoEntity::getCity, city));
    }

    @Override
    public List<UserInfoEntity> findUserByUids(List<Long> userIds) {
        return userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfoEntity.class)
                .in(UserInfoEntity::getUserId, userIds));
    }

    @Override
    public void updateUserInfo(UserInfoEntity build) {
        userInfoMapper.update(build, Wrappers.lambdaUpdate(UserInfoEntity.class)
                .eq(UserInfoEntity::getUserId, build.getUserId()));
    }

    @Override
    public List<LinearVo> findListGroupByIndustry(List<Long> ids) {
        List<LinearVo> vo = new ArrayList<>();
        QueryWrapper<UserInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("industry title, count(industry) amount")
                .in("user_id", ids)
                .groupBy("industry")
                .last("Order By amount DESC");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        maps.forEach(map -> {
            LinearVo linearVo = new LinearVo();
            linearVo.setTitle(Convert.toStr(map.get("title")));
            linearVo.setAmount(Convert.toInt(map.get("amount")));
            vo.add(linearVo);
        });
        return vo;
    }

    @Override
    public List<LinearVo> findListGroupByGender(List<Long> ids) {
        List<LinearVo> vo = new ArrayList<>();
        QueryWrapper<UserInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sex title, count(sex) amount")
                .in("user_id", ids)
                .groupBy("sex");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        maps.forEach(map -> {
            LinearVo linearVo = new LinearVo();
            linearVo.setTitle( Convert.toInt(map.get("title")) == 1 ? "男性用户" : "女性用户");
            linearVo.setAmount(Convert.toInt(map.get("amount")));
            vo.add(linearVo);
        });
        return vo;
    }

    @Override
    public List<LinearVo> findListGroupByAge(List<Long> ids) {
        List<LinearVo> vo = new ArrayList<>();
        QueryWrapper<UserInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("age title, count(age) amount")
                .in("user_id", ids)
                .groupBy("age");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        maps.forEach(map -> {
            LinearVo linearVo = new LinearVo();
            linearVo.setTitle(Convert.toStr(map.get("title")));
            linearVo.setAmount(Convert.toInt(map.get("amount")));
            vo.add(linearVo);
        });
        return vo;
    }

    @Override
    public List<LinearVo> findListGroupByLocation(List<Long> ids) {
        List<LinearVo> vo = new ArrayList<>();
        QueryWrapper<UserInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("city title, count(city) amount")
                .in("user_id", ids)
                .groupBy("city");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        maps.forEach(map -> {
            LinearVo linearVo = new LinearVo();
            linearVo.setTitle(Convert.toStr(map.get("title")));
            linearVo.setAmount(Convert.toInt(map.get("amount")));
            vo.add(linearVo);
        });
        return vo;
    }


}
